// pages/fawnIdleClassifyGoods/fawnIdleClassifyGoods.js
import api from "../../config/api";
import request from "../../utils/request";
import {
    getBarAndTitleHeight
} from "../../utils/utils";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        ...getBarAndTitleHeight(),
        title: '',
        current: 10,
        list: [],
        page: 1,
        total: 0,
        isDone: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            ...getBarAndTitleHeight(),
            id: options.id,
            title: options.title
        })
        this.getList(1)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    // 标签改变
    tabChange(e) {
        if (this.data.current === e.currentTarget.dataset.current) {
            return
        }
        this.setData({
            current: e.currentTarget.dataset.current,
            page: 1,
            isDone: false,
        })
        this.getList(1)
    },
    // 查询数据
    async getList(page) {
        let params = {
            pageIndex: page,
            pageSize: 10,
            classifyId: this.data.id,
            sortType: this.data.current
          }
        let res = await request.get(api.miniGoodsPage, params, { loading: false })
        if (res.httpStatus == 200) {
            this.setData({
                list: page === 1 ? res?.data?.records : [
                    ...this.data.list,
                    ...res?.data?.records,
                ],
                total: res?.data?.pages
            })
        }
        wx.stopPullDownRefresh()
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            page: 1,
            isDone: false,
          })
          this.getList(1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let {
            page,
            total
          } = this.data
          if (page < total) {
            page++
            this.setData({
              page
            })
            this.getList(page)
          } else {
            this.setData({
              isDone: true
            })
          }
    }
})